﻿package view.components.contacts {
	import flash.display.MovieClip;
	import flash.media.Video;
	import flash.net.NetConnection;
	import flash.net.NetStream;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.materials.MovieMaterial;
	import org.papervision3d.materials.VideoStreamMaterial;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.view.BasicView;
	import view.module.ContactsMediator;
	import org.papervision3d.events.InteractiveScene3DEvent; 
	
	public class ContactsTwoSidedPlane extends DisplayObject3D {
		
		/**
		 * Private var
		 */
		private var _mailLinkArea:Plane;
		private var _basicView:BasicView;
		private var _titleMovie:MovieClip;
		private var _aboutLinkArea:Plane;
		private var _backPlane:Plane;
		private var quality:uint = 8;
		private var videoStreamMaterial:VideoStreamMaterial;
		private var netConnection:NetConnection;
		private var netStream:NetStream;
		
		/**
		 * Constructor
		 * @param	$xmlData
		 */
		public function ContactsTwoSidedPlane($xmlData:XMLList) {					
			var data:XML = getAsset(ContactsMediator.MOD_NAME, $xmlData);
			init(data.asset[0], data.asset[1], data.asset[0].@video);
		}
		
		/**
		 * initialized all materials
		 * @param	$contactText
		 * @param	$aboutText
		 * @param	$video
		 */
		private function init($contactText:String = "", $aboutText:String = "", $video:String = ""):void 
		{
			_mailLinkArea = createLinkMail();
			_aboutLinkArea = createLinkAbout();
			
			/**
			 * Create first side of 3d rectangle, information
			 */
			var contactMaterial:ContactMaterial = new ContactMaterial($contactText);
			_titleMovie = contactMaterial.title;
			var frontMaterial:MovieMaterial = new MovieMaterial(contactMaterial, true, true);
			frontMaterial.tiled = true;
			frontMaterial.smooth = true;
			frontMaterial.interactive = true;
			
			/**
			 * Create secont material, about
			 */
			var backMaterial:MovieMaterial = new MovieMaterial(new AboutMaterial($aboutText), true, true);
			backMaterial.smooth = true;
			backMaterial.tiled = true;
			backMaterial.interactive = true;
			
			// Create plane for two material
			var frontPlane:Plane = new Plane(frontMaterial, frontMaterial.movie.width, frontMaterial.movie.height, quality, quality);
			var backPlane:Plane = new Plane(backMaterial, backMaterial.movie.width, backMaterial.movie.height, quality, quality);
			backPlane.rotationY = 180;
			frontPlane.z = 1;
				
			_backPlane = backPlane;
			
			addChild(backPlane);
			addChild(frontPlane, "frontPlane");
			//addChild(createVideoOval($video));
			addChild(_mailLinkArea);
			addChild(_aboutLinkArea);
		}
		
		/**
		 * Simple getting assets
		 * @param	$modName
		 * @param	$xmlData
		 * @return
		 */
		private function getAsset($modName:String, $xmlData:XMLList):XML 
		{
			for each (var item:XML in $xmlData.page) {
				if (item.@mod == $modName) 
					return item;
			}
		}
		
		/**
		 * Create link area material
		 * @return
		 */
		private function createLinkMail():Plane {
			var linkMailMaterial:ColorMaterial = new ColorMaterial(0x000000, 0);
			
			linkMailMaterial.interactive = true;
			var linkPlane:Plane = new Plane(linkMailMaterial, 180, 15);
			linkPlane.y -= 190;
			linkPlane.x += 0;
			linkPlane.z -= 30;
			
			return linkPlane;
		}
		
		private function createLinkAbout():Plane {
			var colorMaterial:ColorMaterial = new ColorMaterial(0x000000, 0);
			
			colorMaterial.interactive = true;
			var linkPlane:Plane = new Plane(colorMaterial, 230, 100);
			linkPlane.y += 180;
			linkPlane.x = 10;
			linkPlane.z -= 30;
			
			return linkPlane;
		}
		
		/**
		 * Create video material
		 * @param	$videoLink
		 * @return plane!
		 */
		private function createVideoOval($videoLink:String):Plane 
		{		
			var customClient:Object = new Object();
			customClient.onMetaData = metaDataHandler;
			
			netConnection = new NetConnection();
			netConnection.connect(null);
			
			netStream = new NetStream(netConnection);
			netStream.client = customClient;
			netStream.play($videoLink);
			var video:Video = new Video();
			video.smoothing = true;
			netStream.pause();
			video.attachNetStream(netStream);
			videoStreamMaterial = new VideoStreamMaterial(video, netStream, false, true);
			
			var plane:Plane = new Plane(videoStreamMaterial, 300, 200, quality, quality);
			var ovalMask:MovieClip = new OvalMaska();
			
			video.mask = ovalMask;
			ovalMask.x += 50; ovalMask.y -=  1;
			plane.x -= 150;	plane.y += 10;
			
			return plane;
		}
		
		private function metaDataHandler(infoObject:Object):void {
			trace('metaDataHandler',infoObject);
		}
		
		/**
		 * GETTERS METHODS
		 */
		public function get linkMailArea():Plane 
		{
			return _mailLinkArea;
		}
		
		public function get aboutLinkArea():Plane
		{
			return _aboutLinkArea;
		}
		
		public function get titleMovie():MovieClip
		{
			return _titleMovie;
		}
		
		public function get aboutPlane():Plane
		{
			return _backPlane;
		}
	}
}